package cn.wolfcode.controller;

import cn.wolfcode.anno.RequiredPermission;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.query.EmployeeQueryObject;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.query.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    //处理所有请求
    //处理显示的请求
    @RequestMapping("/list")
    @RequiredPermission(name = "员工列表",expression = "employee:list")
    public String list(Model model,@ModelAttribute("qo")EmployeeQueryObject q){
        PageInfo<Employee> pageInfo = employeeService.query(q);
        model.addAttribute("pageResult", pageInfo);

        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);
        return "employee/list";
    }
    //处理删除请求
    @RequestMapping("/delete")
    public String delete(Long id){
        employeeService.delete(id);
        return "redirect:/employee/list.do";
    }
    //处理去修改和保存的请求
    @RequestMapping("/input")
    public String input(Long id,Model model){

        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles", roles);

        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);
        if(id!=null){
            Employee employee = employeeService.selectOne(id);
            model.addAttribute("employee", employee);//回显
        }
        return "employee/input";
    }
    //处理修改和保存的请求
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee,Long[] ids){
        if(employee.getId()!=null){
            employeeService.update(employee,ids);
        }
        else{
            employeeService.insert(employee,ids);
        }
        return new JsonResult();
    }

    //进行批量删除
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids){
        employeeService.batchDelete(ids);
        return new JsonResult();
    }

    //验证用户名是否存在
    @RequestMapping("/checkName")
    public HashMap<String,Boolean> checkName(String name){
        Employee employee=employeeService.selectName(name);
        HashMap<String,Boolean> map = new HashMap<>();
        map.put("valid", employee==null);
        return map;
    }

    //改变员工的状态
    @RequestMapping("/changeStatus")
    @ResponseBody
    public JsonResult changeStatus(Long id){
        employeeService.updateStatus(id);
        return new JsonResult();
    }
}